//
//  SISSPictureShowView.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/9/21.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSPictureShowView.h"
#import "SISSBaseCollectionView.h"
#import "SISSComplainCollectionViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SISSPhotoBrowser.h"

@interface SISSPictureShowView ()<UICollectionViewDelegate,UICollectionViewDataSource,MWPhotoBrowserDelegate>

@property (weak, nonatomic) UILabel *titleLabel;
@property (weak, nonatomic) SISSBaseCollectionView *collectionView;

@end
@implementation SISSPictureShowView

- (void)setTitleStr:(NSString *)titleStr {
    _titleStr = titleStr;
    self.titleLabel.text = titleStr;
}

- (void)setImagesUrlArr:(NSArray *)imagesUrlArr {
    _imagesUrlArr = imagesUrlArr;
    if (imagesUrlArr.count * 75 + 10 > SCREEN_WIDTH) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(132);
        }];
    }
    
    if (imagesUrlArr.count * 75 + 10 > 2 * SCREEN_WIDTH) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(193);
        }];
    }
    [self.collectionView reloadData];
}

- (void)setImages:(NSArray *)images {
    _images = images;
    if (images.count * 75 + 10 > SCREEN_WIDTH) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(132);
        }];
    }
    
    if (images.count * 75 + 10 > 2 * SCREEN_WIDTH) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(193);
        }];
    }
    
    [self.collectionView reloadData];
}

- (void)initUI {
    [super initUI];
    
    self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = MAIN_TEXT_COLOR;
    titleLabel.text = @"房源实景照片";
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    //    CGFloat itemW = (self.collectionView.bounds.size.width - 3 * 1) / 3;
    //    CGFloat itemH = (self.collectionView.bounds.size.height - 3 * 1) / 3 ;
    flowLayout.itemSize = CGSizeMake(65, 65);
    flowLayout.sectionInset = UIEdgeInsetsMake(1, 10, 1, 10);
    SISSBaseCollectionView *collectionView = [[SISSBaseCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView registerNib:[UINib nibWithNibName:@"SISSComplainCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ID"];
    [self addSubview:collectionView];
    self.collectionView = collectionView;
    
    WeakSelf
    CGFloat margin = 8.0;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(margin);
        make.top.equalTo(weakSelf).offset(margin);
        make.height.mas_equalTo(26);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf);
        make.top.equalTo(weakSelf.titleLabel.mas_bottom).offset(margin);
        make.height.mas_equalTo(8 * margin);
        make.bottom.equalTo(weakSelf).offset(-margin);
    }];
    
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return  self.images.count > 0 ? self.images.count : self.imagesUrlArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSComplainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ID" forIndexPath:indexPath];
    
    if (self.images.count > 0) {
        cell.imageView.image = self.images[indexPath.row];
    } else {
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.imagesUrlArr[indexPath.item]]];
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    
    UIResponder *responder = self;
    UIViewController *viewController;
    while ((responder = [responder nextResponder]))
        if ([responder isKindOfClass: [UIViewController class]])
            viewController = (UIViewController *)responder;
    
    MWPhotoBrowser *photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    [photoBrowser setCurrentPhotoIndex:indexPath.item];
//    [photoBrowser showNextPhotoAnimated:YES];
//    [photoBrowser showPreviousPhotoAnimated:YES];
    SISSNavigationController *navigationController = [[SISSNavigationController alloc] initWithRootViewController:photoBrowser];
    navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
    [viewController presentViewController:navigationController animated:YES completion:nil];
}


- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.images.count > 0 ? self.images.count : self.imagesUrlArr.count;
}

- (id<MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    MWPhoto *photo;
    // 创建图片模型
    photo = self.images.count > 0 ? [MWPhoto photoWithImage:self.images[index]] : [MWPhoto photoWithURL:[NSURL URLWithString:self.imagesUrlArr[index]]];
    return photo;
}

@end
